<?php
namespace App\Http\Controllers;

use App\Http\Logic\UserLogic;
use App\Http\Requests\LoginRequest;

class  LoginController extends BaseController
{
    /**
     * @notes 微信用户登录
     * @param LoginRequest $request
     * @return \Dingo\Api\Http\Response
     * @throws \Illuminate\Http\Client\RequestException
     * @author Evil丿
     * @date 2024/08/12 17:08
     */
    public function loginByMiniWechat(LoginRequest $request)
    {
        $code = $request->input('code');

        $res = UserLogic::loginByOpenId($code, 0);

        if($res === false) {
            return $this->error(UserLogic::getErrorMessage());
        }

        return $this->success($res['data'],$res['message'], $res['code']);
    }

    /**
     * @notes 支付宝用户登录
     * @param LoginRequest $request
     * @return \Dingo\Api\Http\Response
     * @author Evil丿
     * @date 2024/08/14 14:31
     */
    public function loginByMiniAliPay(LoginRequest $request) {
        $code = $request->input('code');

        $res = UserLogic::loginByOpenId($code, 1);

        if($res === false) {
            return $this->error(UserLogic::getErrorMessage());
        }

        return $this->success($res['data'],$res['message'], $res['code']);
    }

    /**
     * @notes 获取微信用户手机号
     * @param LoginRequest $request
     * @return \Dingo\Api\Http\Response
     * @throws \Illuminate\Http\Client\RequestException
     * @author Evil丿
     * @date 2024/08/12 17:08
     */
    public function getMobileByMiniWechat(LoginRequest $request)
    {
        $code = $request->input('code');

        $cache = $request->input('cache');

        $res = UserLogic::registerByMobile($code, $cache, 0);

        if($res === false) {
            return $this->error(UserLogic::getErrorMessage());
        }

        return $this->success();
    }

    /**
     * @notes 获取支付宝用户手机号
     * @param LoginRequest $request
     * @return \Dingo\Api\Http\Response
     * @author Evil丿
     * @date 2024/08/14 15:29
     */
    public function getMobileByMiniAliPay(LoginRequest $request)
    {
        $response = $request->input('response');

        $cache = $request->input('cache');

        $res = UserLogic::registerByMobile($response, $cache, 1);

        if($res === false) {
            return $this->error(UserLogic::getErrorMessage());
        }

        return $this->success();
    }

    public function getUserToken(LoginRequest $request)
    {
        $id = $request->input('id');
        $type = $request->input('type');

        $res = UserLogic::getLoginToken($id, $type);

        if($res === false) {
            return $this->error(UserLogic::getErrorMessage());
        }

        return $this->success($res);
    }
}
